﻿/*
    Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
    Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html) 
 */

using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace IvmpEditor
{
    /// <summary>
    /// Interaction logic for wndOptions.xaml
    /// </summary>
    public partial class wndOptions : Window
    {
        public wndOptions()
        {
            InitializeComponent();

            comboBoxLanguage.SelectionChanged +=
                delegate
                {
                    // Apply selected localization to some controls
                    var culture = GetSelectedLanguage(CultureInfo.InstalledUICulture);    
                    labelTranslatorTag.Content = IvmpEditor.Properties.Resources.ResourceManager.GetString("L_TranslatorTag", culture);
                    labelLocalizationNote.Content = IvmpEditor.Properties.Resources.ResourceManager.GetString("L_OPTIONS_LocalizationNote", culture);
                    buttonSaveSettings.Content = IvmpEditor.Properties.Resources.ResourceManager.GetString("L_Save", culture);
                };

            this.SourceInitialized +=
                delegate
                {
                    // Pick current language and update comboBox:
                    var strCulture = Settings.LocalizeCulture;
                    var i = 0;
                    switch (strCulture.ToLower())
                    {
                        case "en-gb": i = 0; break; // English
                        case "ru-ru": i = 1; break; // Russian
                        case "hu-hu": i = 2; break; // Hungarian
                        case "fr-fr": i = 3; break; // French
                        case "de-de": i = 4; break; // German
                    }
                    comboBoxLanguage.SelectedIndex = i;
                    comboBoxLanguage.UpdateLayout();

                    // Pawn includes dir:
                    textBoxPawnCompilerIncludesDir.Text = Settings.PawnIncludesDir;

                    // Available squirrel compilers:
                    var sqCs = SquirrelCompiler.ListAvailableSquirrelCompilers(Directory.GetCurrentDirectory());
                    foreach (var sqc in sqCs)
                        comboBoxSquirrelCompilers.Items.Add(new ComboBoxItem()
                            {
                                Content = sqc,
                                IsSelected = Settings.SquirrelEXEName == sqc
                            });

                    // Various options:
                    checkBoxTabs.IsChecked = Settings.MultiTabbedInterface;
                    checkBoxRememberWindowState.IsChecked = Settings.RememberWindowState;
                    checkBoxRestoreTabs.IsChecked = Settings.RestoreOpenedTabs;
                };
        }
        CultureInfo GetSelectedLanguage(CultureInfo defaultValue)
        {
            switch (comboBoxLanguage.SelectedIndex)
            {
                case 0: return CultureInfo.GetCultureInfo("en-gb"); // English
                case 1: return CultureInfo.GetCultureInfo("ru-ru"); // Russian
                case 2: return CultureInfo.GetCultureInfo("hu-hu"); // Hungarian
                case 3: return CultureInfo.GetCultureInfo("fr-fr"); // French
                case 4: return CultureInfo.GetCultureInfo("de-de"); // German
            }

            return defaultValue;
        }
        private void buttonDefaultIncludesDir_Click(object sender, RoutedEventArgs e)
        {
            textBoxPawnCompilerIncludesDir.Text = "..\\pawno\\include"; // for sa-mp servers
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void buttonSaveSettings_Click(object sender, RoutedEventArgs e)
        {
            // Language / localization:
            var culture = GetSelectedLanguage(null);
            Settings.LocalizeCulture = culture != null ? culture.IetfLanguageTag : "auto";

            // Pawn compiler includes dir:
            Settings.PawnIncludesDir = textBoxPawnCompilerIncludesDir.Text ?? "..\\pawno\\include";

            // Squirrel version:
            Settings.SquirrelEXEName = comboBoxSquirrelCompilers.Text;

            // Various options:
            Settings.MultiTabbedInterface = checkBoxTabs.IsChecked.Value;
            Settings.RememberWindowState = checkBoxRememberWindowState.IsChecked.Value;
            Settings.RestoreOpenedTabs = checkBoxRestoreTabs.IsChecked.Value;

            Close();
        }
    }
}
